﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Media.Imaging;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Helpers;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using TestApp.Model;
using TestApp.Messages;
using TestApp.Resources;

namespace TestApp.ViewModel
{
    public class CouponDetailsViewModel : ViewModelBase
    {
        // Constants
        private const string EXPIRATION_DATE_FORMAT = "MM/dd/yyyy";

        // Property Names
        public const string CouponImagePropertyName = "CouponImage";
        public const string ButtonTextPropertyName = "ButtonText";
        public const string IsEnabledPropertyName = "IsEnabled";


        // Private attirbutes for properties with custom get/set
        private BitmapImage _couponImage = null;

        // Coupon Receiver
        MessageReceiver<CouponInitMessage> CouponReceiver;

        // Command Button Is Pressed
        public RelayCommand AddButtonPressed
        {
            set;
            get;
        }

        // Coupon
        public Coupon CurrentCoupon
        {
            set;
            get;
        }

        // Date Formatted String
        public String FormattedCouponDate
        {
            get
            {
                // "Coupon Expires" should be localized
                return LocalizedStrings.CouponExpires + CurrentCoupon.ExpirationDate.ToString(EXPIRATION_DATE_FORMAT);
            }
        }

        // Image Related Methods
        public BitmapImage CouponImage
        {
            private set
            {
                _couponImage = value;
                CurrentCoupon.CouponImage = value;
                RaisePropertyChanged(CouponImagePropertyName);
            }
            get
            {
                if (_couponImage == null)
                {
                    LoadImage();
                    return null;
                }
                return _couponImage;
            }
        }

        public void LoadImage()
        {
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(ImageLoaded);
            webClient.OpenReadAsync(new Uri(CurrentCoupon.ImageUrl), webClient);
        }

        private void ImageLoaded(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null && !e.Cancelled)
            {
                try
                {
                    BitmapImage image = new BitmapImage();
                    image.SetSource(e.Result);
                    CouponImage = image;
                }
                catch (Exception ex)
                {
                    //Exception handle appropriately for your app  
                }
            }
            else
            {
                //Either cancelled or error handle appropriately for your app  
            }
        }

        // Button text
        public String ButtonText
        {
            get
            {
                // The Text should be localized. couponIsAdded should be a property in coupon.
                if (CurrentCoupon.State.CompareTo("Available") == 0)
                {
                    return LocalizedStrings.CouponLoadToCard;
                }
                else
                {
                    return LocalizedStrings.CouponAdded;
                }
            }
        }

        // IsEnabled button property
        public bool IsEnabled
        {
            get
            {
                return (CurrentCoupon.IsRemovable || CurrentCoupon.State.CompareTo("Taken") != 0);
            }
        }

        // Details Screen Summary Text Box
        public String SummaryText
        {
            get
            {
                return CurrentCoupon.Name + " " + CurrentCoupon.Summary;
            }
        }
        
        // Constructor for the ListBox ItemSource
        public CouponDetailsViewModel(Coupon coupon)
        {
            CurrentCoupon = coupon;
            AddButtonPressed = new RelayCommand(LoadToCardPressed);
        }

        // Constructor for the DetailsView
        public CouponDetailsViewModel() 
        {
            CouponReceiver = new MessageReceiver<CouponInitMessage>((message) =>
            {
                CurrentCoupon = message.Value;
            }, true);
            AddButtonPressed = new RelayCommand(LoadToCardPressed);
        }

        // Load to Card Pressed
        private void LoadToCardPressed()
        {
            if (CurrentCoupon.State.CompareTo("Available") == 0)
            {
                CurrentCoupon.State = "Taken";
            }
            else
            {
                CurrentCoupon.State = "Available";
            }
            RaisePropertyChanged(ButtonTextPropertyName);
            RaisePropertyChanged(IsEnabledPropertyName);
        }
    }
}
